using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MyApp.WebApi.Entity;
using MyApp.WebApi.ParamModel;
using MyApp.WebApi.Repository;
using MyApp.WebApi.Utils;

namespace Admin3000.Backend.Controllers
{
    //支付方式列表
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class PayWaysController : ControllerBase
    {
        private IRepository<PayWays> _payWaysRespository;

        private IRepository<UsersOrders> _usersOrdersRespository;

        private IRepository<Users> _usersRespository;
        private IRepository<Shoppings> _shoppingsRespository;



        private TokenParameter _tokenParameter;

        private readonly IConfiguration _configuration;

        public PayWaysController(
            IConfiguration configuration,
            IRepository<PayWays> payWaysRespository,
            IRepository<UsersOrders> usersOrdersRespository,
            IRepository<Users> usersRespository,
            IRepository<Shoppings> shoppingsRespository
        )
        {
            _configuration = configuration;
            _payWaysRespository = payWaysRespository;
            _usersOrdersRespository = usersOrdersRespository;
            _usersRespository = usersRespository;
            _shoppingsRespository = shoppingsRespository;

            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }

        //获取支付方式列表
        [HttpGet, Route("getlist")]
        public dynamic GetList()
        {
            // var list =
            //     from payways in _payWaysRespository.Table
            //     join userOders
            //     in _usersOrdersRespository.Table
            //     on payways.Id
            //     equals userOders.PayWaysId
            //     join users
            //     in _usersRespository.Table
            //     on userOders.UsersId
            //     equals users.Id
            //     join shopping in _shoppingsRespository.Table on users.ShoppingsId equals shopping.Id
            //     where payways.IsDeleted == false
            //     select
            //     new
            //     {
            //         PayWaysId = payways.Id,
            //         PayWays = payways.Ways,
            //         UserName = users.Username,
            //         ShoppingSum=shopping.Sum

            //     };
            var list = _payWaysRespository.Table.ToList();
            var res =
                new { Code = 200, Data = list, Msg = "获取支付列表成功!" };

            return JsonHelper.Serialize(res);
        }


        // 通过商品Id返回对应的商品的购买的总价格=单价x数量
        [HttpGet, Route("getGood/{id}")]
        public dynamic GetGoods(int id)
        {
            var list =
                from payways in _payWaysRespository.Table
               join userOders
               in _usersOrdersRespository.Table
               on payways.Id
               equals userOders.PayWaysId
               join users
               in _usersRespository.Table
               on userOders.UsersId
               equals users.Id
               join shopping in _shoppingsRespository.Table on users.ShoppingsId equals shopping.Id
               where payways.IsDeleted == false
               select
               new
               {
                   PayWaysId = payways.Id,
                   PayWays = payways.Ways,
                   UserName = users.Username,
                   ShoppingSum = shopping.Sum

               };

            var res =
             new { Code = 200, Data = list, Msg = "获取支付列表成功!" };

            return JsonHelper.Serialize(res);
        }

        //支付方式表分页
        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;
            var rowsTotal = pager.RowsTotal;

            // 获得所有满足条件的记录
            var users =
                _payWaysRespository.Table.Where(x => x.IsDeleted == false);

            //  var users = from program in _payWaysRespository.Table
            //             join articleprogram in _articleRespository.Table on program.Id equals articleprogram.ProgramId
            //             orderby articleprogram.Id
            //             select new
            //             {
            //                 Id = program.Id,
            //                IsActived = program.IsActived,
            //                 IsDeleted = program.IsDeleted,
            //                 CreatedTime = program.CreatedTime,
            //                 Remarks = program.Remarks
            //             };
            // 对获得的记录分页
            var u =
                users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res =
                new
                {
                    Code = 1000,
                    Data =
                        new
                        {
                            Data = u,
                            Pager =
                                new
                                {
                                    pageIndex,
                                    pageSize,
                                    rowsTotal = users.Count()
                                }
                        },
                    Msg = "获取支付方式列表成功^_^"
                };
            return res;
        }

        //获取支付方式表指定Id信息
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var AritpId = _payWaysRespository.GetById(id);

            if (AritpId == null)
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "指定支付方式不存在"
                };
            }
            return new
            {
                Code = 1000,
                Data = AritpId,
                Msg = "获取支付方式列表成功^_^"
            };
        }

        // 支付方式表添加
        [HttpPost]
        public string Post(NewPayWays model)
        {
            var ways = model.Ways.Trim();
            var same =
                _payWaysRespository
                    .Table
                    .Where(x => x.Ways == ways)
                    .ToList()
                    .FirstOrDefault();

            if (string.IsNullOrEmpty(ways))
            {
                var tmp =
                    new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "支付方式不能为空，请确认后重试"
                    };
                return JsonHelper.Serialize(tmp);
            }
            else
            {
                if (same != null)
                {
                    return JsonHelper
                        .Serialize(new
                        {
                            Code = 400,
                            Data = "",
                            Msg = "该支付方式已存在"
                        });
                }
                else
                {

                    var user = new PayWays { Ways = model.Ways };

                    _payWaysRespository.Insert(user);

                    var res =
                        new { Code = 1000, Data = user, Msg = "添加支付方式成功" };
                    return JsonHelper.Serialize(res);
                }
            }

        }

        // 支付方式表修改指定id
        [HttpPut("{id}")]
        public dynamic Put(int id, NewPayWays model)
        {
            var PayWaysId = _payWaysRespository.GetById(id);

            if (PayWaysId != null)
            {
                PayWaysId.Ways = model.Ways;

                // user.Password = model.Password;
                PayWaysId.UpdatedTime = DateTime.Now;
                _payWaysRespository.Update(PayWaysId);
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = PayWaysId,
                        Msg =
                            string
                                .Format("你修改支付方式的id为：{0}，已经修改成功，请注意查收",
                                id)
                    });
            }
            else
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 104,
                        Data = "",
                        Msg = "指定Id的支付方式不存在，请确认后重试"
                    });
            }
        }

        // 支付方式表删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var AritpId = _payWaysRespository.GetById(id);
            if (AritpId != null)
            {
                // _payWaysRespository.Delete(id);
                AritpId.IsDeleted = true;
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的支付方式成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的支付方式不存在，请确认后重试"
                };
            }
        }
    }
}
